#include "unp.h"

int main(int argc, char *argv[])
{
    int sockfd, n;
    struct sockaddr_in servaddr;
    char buf[MAXLINE+1];
    int count = 0;
    if (argc != 2) {
        err_quit("usage: a.out <IPAddress>");
    }

    if ((sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
        err_sys("socket error");
    }
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(8000);
    if (inet_pton(AF_INET, (const char*)argv[1], &servaddr.sin_addr) != 1) {
        err_quit("inet_pton() error for %s", argv[1]);
    }
    if (connect(sockfd, (SA*)&servaddr, sizeof(servaddr)) < 0) {
        err_sys("connect error");
    }
    while ((n = recv(sockfd, buf, MAXLINE, 0)) > 0) {
        buf[n] = 0;
        fputs(buf,stdout);
        print_hex("buf", buf, n);
        printf("The %d times\n", ++count);
    }
    close(sockfd);

    return 0;
}
